package com.safety51.bootstrap.demo.domain.student;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by liuyuancheng on 2021/9/10  <br/>
 *
 * @author liuyuancheng
 */
@Service
public class MultiDataSourceService {

    @Autowired
    @Qualifier(value = "dataSourceOne")
    private DataSource dataSource1;

    @Autowired
    @Qualifier(value = "dataSourceTwo")
    private DataSource dataSource2;

    @Autowired
    @Qualifier(value = "dataSourceThree")
    private DataSource dataSource3;

    public List<String> dataSources(){
        try {
            return Arrays.asList(dataSource1.getConnection().getMetaData().getURL(),
                    dataSource2.getConnection().getMetaData().getURL(),
                    dataSource3.getConnection().getMetaData().getURL()
            );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return Collections.emptyList();
    }

}
